package com.jackchong.base;

import android.app.DialogFragment;
import android.content.Context;
import android.os.Bundle;
import androidx.annotation.Nullable;

import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.jackchong.utils.JAuto;;
import com.jackchong.widget.JView;
import com.utils.R;


/**
 * Created by lyf on 2019/2/25.
 */

public abstract class BaseDialogFragment extends DialogFragment {

    protected BaseToolsActivity mActivity;
    private float mAlpha = 0.4f;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mActivity = (BaseToolsActivity) context;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_FRAME, R.style.dialog_fragment);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View rootView = JView.inflate(mActivity, getContentLayout(), null);
        getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        getDialog().setCanceledOnTouchOutside(true);
        if (onAutoLayout(rootView)) JAuto.auto(rootView, (view, id) -> onAutoLayout(view));
        init(rootView);
        return rootView;
    }

    public void setAlpha(int alpha){
        mAlpha = alpha;
    }


    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams lp= window.getAttributes();
        lp.gravity = Gravity.CENTER;
//        lp.dimAmount = 1 - mAlpha;
        lp.flags |= WindowManager.LayoutParams.FLAG_DIM_BEHIND;
        window.setAttributes(lp);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    }



    protected boolean onAutoLayout(View view){
        return true;
    }


    public abstract int getContentLayout();

    public abstract void init(View view);
}
